package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.entity.Auth;
import com.atguigu.crowd.entity.Role;
import com.atguigu.crowd.service.api.AdminService;
import com.atguigu.crowd.service.api.AuthService;
import com.atguigu.crowd.service.api.RoleService;
import com.atguigu.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class AssignHandler {

	@Autowired
	private AdminService adminService;

	@Autowired
	private RoleService roleService;

	@Autowired
	private AuthService authService;


	/**
	 * 给角色分配权限（菜单），这里就是权限
	 * @param listMap
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/assign/do/role/assign/auth.json")
	public ResultEntity<String> assignDoRoleAssignAuth(@RequestBody Map<String,List<Integer>> map){

		//给角色分配权限
		authService.savaRoleAuthRelathinShip(map);

		return ResultEntity.successWithoutData();
	}
	
	@ResponseBody
	@RequestMapping("/assign/get/assigned/auth/id/by/role/id.json")
	public ResultEntity<List<Integer>> getAssignedAuthIdByRole(
			@RequestParam("roleId")Integer roleId){
		//根据角色id查询出该角色id的 权限id
		List<Integer>authIdList=authService.getAssignedAuthIdByRoleId(roleId);
		return ResultEntity.successWithData(authIdList);
	}

	@ResponseBody
	@RequestMapping("/assgin/get/all/auth.json")
	public ResultEntity<List<Auth>>getAllAuth(){

		/*try {*/

			List<Auth> list = authService.getAll();

			//List<Auth> listSql = authService.getAllBySql();
		
			//List<Role> list = roleService.getAll();
		
			System.out.println(list);

			return ResultEntity.successWithData(list);

/*		}catch (Exception e){
			return ResultEntity.failed("报错了");
		}*/

	}

	/**
	 * 执行用户分配角色关系
	 * @return
	 */
	@RequestMapping("/assign/do/role/assign.ht")
	public String saveAdminRoleRelationship(
			@RequestParam("adminId") Integer adminId,
			@RequestParam("pageNum") Integer pageNum,
			@RequestParam("keyword") String keyword,
			//我们允许用户在页面上取消所有已分配的角色，所以其实也可以不提供 roleIdList请求参数
			//required = false 表示这个请求参数不是必须的
			@RequestParam(value = "roleIdList",required = false) List<Integer> roleIdList
	){
		adminService.saveAdminRoleRalationship(adminId,roleIdList);

		return "redirect:/admin/get/page.ht?pageNum="+pageNum+"&keyword="+keyword;
	}

	/**
	 * 跳转到给用户分配角色的页面去
	 * @param adminId
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/assign/to/assign/role/page.ht")
	public String toAssignRolePage(@RequestParam("adminId")Integer adminId, ModelMap modelMap,@RequestParam("loginAcct") String loginAcct){

		//1、查询已经分配的角色
		List<Role> assignRoleList=roleService.getAssignedRole(adminId);

		//2、查询未分配的角色
		List<Role>unAssignRoleList=roleService.getUnAssignedRole(adminId);

		//3、存入模型
		modelMap.addAttribute("assignRoleList",assignRoleList);
		modelMap.addAttribute("unAssignRoleList",unAssignRoleList);
		modelMap.addAttribute("loginAcct",loginAcct);

		//4、返回
		return "admin/assign-role";

	}

}





















